//
//  HongbaoViewController.m
//  盘盈贷1.2
//
//  Created by iOS on 15/3/13.
//  Copyright (c) 2015年 iOS. All rights reserved.
//

#import "HongbaoViewController.h"
#import "AFHTTPRequestOperationManager.h"
#import "Hongbao.h"
#import "HongbaoTableViewCell.h"
@interface HongbaoViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSMutableArray *Array;
@property (nonatomic, retain)UILabel *topLabel;
@end

@implementation HongbaoViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        self.navigationItem.hidesBackButton = NO;
        self.title = @"红包";

        self.Array = [NSMutableArray array];
        
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//    UIView *top = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 3.8)];
//    //top.backgroundColor = [UIColor redColor];
//    [self.view addSubview:top];
//    [top release];
//    self.topLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 3.8)];
//    self.topLabel.backgroundColor = [UIColor whiteColor];
//    self.topLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:40];
//    self.topLabel.textColor = [UIColor redColor];
//   // self.topLabel.backgroundColor = [UIColor greenColor];
//    self.topLabel.textAlignment = NSTextAlignmentCenter;
//    [top addSubview:self.topLabel];
//    [self.topLabel release];
    

    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.scrollEnabled = YES;
   // self.tableView.tableHeaderView = top;
    [self.view addSubview:self.tableView];
    //网络请求 分开 有利于维护 和阅读
    [self HttpResponseObject];
    [self.tableView release];
   
   
}
#pragma 网络请求具体实现方式
- (void)HttpResponseObject
{
    //NSString *str = @"http://www.cloudpeng.com:8102/event/getcoupon?username=chenying";
    NSString *str = [NSString stringWithFormat:@"http://www.cloudpeng.com:8102/event/getcoupon?strusername=%@", self.strusername];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    //manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
       
        //int i = 0;
        for (NSDictionary * dic in responseObject) {
            Hongbao *model = [[Hongbao alloc] initWithDictionary:dic];
//                 i += [dic[@"Available"] integerValue];
////              i += dic[@"Available"];
////            i += [dic objectForKey:@"Available"];
//           
            [self.Array addObject:model];
            //这里已经把你遍历出来的model里米看的 available的属性自动加1了；
            NSLog(@"model.available = %d",model.Available);
           //  model.Available = i;
            
           
        }
        
       // self.topLabel.text = [NSString stringWithFormat:@"%d", i];
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"~~~~~~~%@",error);
    }];
    
}
//设置tableview 每个分区(section)显示多少行(row)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return [self.Array count];
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    HongbaoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"3"];
    if (cell == nil) {
        cell = [[HongbaoTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"3"];
    }
    
    if ([[[self.Array objectAtIndex:indexPath.row] Amount] integerValue] > 1000) {
        NSMutableString *t = [NSMutableString stringWithFormat:@"红包金额 :%@.00元", [[self.Array objectAtIndex:indexPath.row] Amount]];
        [t insertString:@"," atIndex:7];
        cell.jine.text = t;
    }else {
        cell.jine.text = [NSString stringWithFormat:@"红包金额:%@(元)", [[self.Array objectAtIndex:indexPath.row] Amount]];
    }
    if ([[self.Array objectAtIndex:indexPath.row] Available] > 1000) {
        NSMutableString *t1 = [NSMutableString stringWithFormat:@"红包余额 :%d.00元", [[self.Array objectAtIndex:indexPath.row] Available]];
        [t1 insertString:@"," atIndex:7];
        cell.yue.text = t1;
    }else {
        cell.yue.text = [NSString stringWithFormat:@"红包余额:%d.00(元)", [[self.Array objectAtIndex:indexPath.row] Available]];
    }
   
//    cell.type.text = [NSString stringWithFormat:@"类型:%@", [[self.Array objectAtIndex:indexPath.row] Type]];
    
    if ([[NSString stringWithFormat:@"%@",[[self.Array objectAtIndex:indexPath.row] Type]]isEqual:@"0"]) {
        cell.type.text = [NSString stringWithFormat:@"类型: %@", @"所有"];
        
    }else if ([[NSString stringWithFormat:@"%@",[[self.Array objectAtIndex:indexPath.row] Type]]isEqual:@"1"]){
         cell.type.text = [NSString stringWithFormat:@"类型: %@", @"注册送红包"];
    }else if ([[NSString stringWithFormat:@"%@",[[self.Array objectAtIndex:indexPath.row] Type]]isEqual:@"2"]){
         cell.type.text = [NSString stringWithFormat:@"类型: %@", @"邀请好友送红包"];
    }else if ([[NSString stringWithFormat:@"%@",[[self.Array objectAtIndex:indexPath.row] Type]]isEqual:@"3"]){
         cell.type.text = [NSString stringWithFormat:@"类型: %@", @"首次充值金额投资送红包"];
    }else if ([[NSString stringWithFormat:@"%@",[[self.Array objectAtIndex:indexPath.row] Type]]isEqual:@"4"]){
       cell.type.text = [NSString stringWithFormat:@"类型: %@", @"首次投资送推荐人红包"];
    }else {
        cell.type.text = [NSString stringWithFormat:@"类型: %@", @"其他"];
    }
    
    
//    cell.zhangtai.text = [NSString stringWithFormat:@"状态:%@", [[self.Array objectAtIndex:indexPath.row] Status]];
    NSString *time = [[self.Array objectAtIndex:indexPath.row] ExpireTime];
    NSArray *timearray = [time componentsSeparatedByString:@"T"]; //从字符A中分隔成2个元素的数组
    cell.shiyongtime1.text = [NSString stringWithFormat:@"%@",  [timearray objectAtIndex:0]];
    NSLog(@"%@", [[self.Array objectAtIndex:indexPath.row] UsedTime]);
    if ([[NSString stringWithFormat:@"%@",[[self.Array objectAtIndex:indexPath.row] UsedTime]]isEqualToString:@"<null>"]) {
        cell.youxiaoqi.text = [NSString stringWithFormat:@"使用时间: %@",  @"未使用"];
    } else {
        NSString *time1 = [[self.Array objectAtIndex:indexPath.row] UsedTime];
        NSArray *timearray1 = [time1 componentsSeparatedByString:@"T"]; //从字符A中分隔成2个元素的数组
        cell.youxiaoqi.text = [NSString stringWithFormat:@"使用时间: %@", [timearray1 objectAtIndex:0]];
    }
    
//    NSString *time1 = [[self.Array objectAtIndex:indexPath.row] CreateTime];
//    NSArray *timearray1 = [time1 componentsSeparatedByString:@"T"]; //从字符A中分隔成2个元素的数组
//   
//    cell.shiyongtime1.text = [NSString stringWithFormat:@"%@",  [timearray1 objectAtIndex:0]];
      return cell;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
